RemoveUserCommandHandler.execute   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 13
rs 9.85
c 0
b 0
f 0
cc 3
1
import { Inject } from '@nestjs/common';
2
import { CommandHandler } from '@nestjs/cqrs';
3
import { CantRemoveYourselfException } from 'src/Domain/User/Exception/CantRemoveYourselfException';
4
import { UserNotFoundException } from 'src/Domain/User/Exception/UserNotFoundException';
5
import { IUserRepository } from 'src/Domain/User/Repository/IUserRepository';
6
import { RemoveUserCommand } from './RemoveUserCommand';
7
8
@CommandHandler(RemoveUserCommand)
9
export class RemoveUserCommandHandler {
10
  constructor(
11
    @Inject('IUserRepository')
12
    private readonly userRepository: IUserRepository,
13
  ) {}
14
15
  public async execute({ id, currentUserId }: RemoveUserCommand): Promise<void> {
16
    if (id === currentUserId) {
17
      throw new CantRemoveYourselfException();      
18
    }
19
20
    const user = await this.userRepository.findOneById(id);
21
22
    if (!user) {
23
      throw new UserNotFoundException();
24
    }
25
26
    await this.userRepository.remove(user);
27
  }
28
}
29